package com.oliver.picturebackendcommon.api.imagesearch.bing;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;

@Slf4j
public class ImageSearch {

    public static void main(String[] args) throws IOException {
        //  测试图片
        String pictureUrl = "https://oliver-1333180070.cos.ap-guangzhou.myqcloud.com/public/1866469198650163201/2025-01-17_ZamooyhFN1J2jFGn.webp";

        String subscriptionKey = "YOUR_SUBSCRIPTION_KEY";
        String endpoint = "https://api.bing.microsoft.com/v7.0/images/search";
        //  构建临时文件
//        File imageFile = new File("path_to_your_image.jpg");
        File imageFile = null;
        try {
            imageFile = File.createTempFile(pictureUrl, null);

            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost request = new HttpPost(endpoint);

            // 设置请求头
            request.addHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
            request.setHeader("Content-Type", "multipart/form-data");

            // 构建 multipart 实体
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addBinaryBody("image", imageFile, ContentType.APPLICATION_OCTET_STREAM, imageFile.getName());
            HttpEntity entity = builder.build();
            request.setEntity(entity);

            try (CloseableHttpResponse response = httpClient.execute(request)) {
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity != null) {
                    String result = EntityUtils.toString(responseEntity);

                    // 在获取到result字符串后
                    ObjectMapper objectMapper = new ObjectMapper();
                    JsonNode rootNode = objectMapper.readTree(result);
                    JsonNode imagesNode = rootNode.path("value");
                    for (JsonNode image : imagesNode) {
                        String url = image.path("contentUrl").asText();
                        String title = image.path("name").asText();
                        System.out.println("Title: " + title);
                        System.out.println("URL: " + url);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {

        } finally {
            if (imageFile != null) {
                // 删除临时文件
                boolean delete = imageFile.delete();
                if (!delete) {
                    log.error("file delete error, filepath = {}", imageFile);
                }
            }
        }

    }
}